<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item
        label="用户ID"
        prop="uid"
      >
        <el-input
          v-model="dataForm.uid"
          required 
          placeholder="用户ID"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="商品ID"
        prop="spuId"
      >
        <el-input
          v-model="dataForm.spuId"
          required 
          placeholder="商品ID"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="商品名称"
        prop="spuName"
      >
        <el-input
          v-model="dataForm.spuName"
          required 
          placeholder="商品名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="商品图片"
        prop="spuImg"
      >
        <single-upload v-model="dataForm.spuImg"></single-upload>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from './singleUpload'
export default {
  components: { SingleUpload },
  data () {
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        uid: '',
        spuId: '',
        spuName: '',
        spuImg: '',
        createTime: '',
        newTime: ''
      },
      dataRule: {
        uid: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
        spuId: [
          { required: true, message: '商品ID不能为空', trigger: 'blur' }
        ],
        spuName: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' }
        ],
        spuImg: [
          { required: true, message: '商品图片地址不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/sys/role/list'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.roleList = data.page.list
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/user/collection/info/${this.dataForm.id.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.uid = data.collection.uid
              this.dataForm.spuId = data.collection.spuId
              this.dataForm.spuName = data.collection.spuName
              this.dataForm.spuImg = data.collection.spuImg
              this.dataForm.createTime = data.collection.createTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      // this.$options.methods.getCurrentTime()
      this.newTime = new Date()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/user/collection/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id.id || undefined,
              'uid': this.dataForm.uid,
              'spuId': this.dataForm.spuId,
              'spuName': this.dataForm.spuName,
              'spuImg': this.dataForm.spuImg,
              'createTime': !this.dataForm.id ? this.newTime : this.dataForm.createTime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
